var $ = require('jquery');
var util = require('imutil');
var Backbone = require('backbone');
var tpl = require('../tpl');
var chatImgUtil = require('../biz/chatImgUtil');

var Messages = require('../model/messages');

var ChatMsgView = require('./chatMsg');
var TimeStampView = require('./timestamp');
var InquirySendView = require('./inquirySend');
var prodlinkView = require('./prodlink');

var dialog = require('dialog');
var bus = require('bus');
var scrollTop = 99999;

module.exports = Backbone.View.extend({
    el: null,
    collection: null,
    template: '',
    events: {

    },
    initialize: function(options, api, uid,productId){
        var that = this;
        this.api = api;
		this.uid = uid;
        this.productId = productId;

        this.api.sdk.getUserById(this.api.sdk.userId,function(user){
            that.myHeadImg=user.avatar;
            that.api.sdk.getProfile(that.uid,function(user){
                that.otherHeadImg = user.avatar;

                that.render();

                if(!that.api.sdk.isUserOnline(user) && user.isSupplier){
                    that.api.sdk.getInquiryLink(that.uid,that.productId,function(inquiry){
                        new InquirySendView({
                            el: that.el,
                            inquiryUrl:inquiry
                        });
                        setTimeout(function(){
                            that.$el.scrollTop(scrollTop);
                        },0);
                    })
                }
                if(that.productId && that.productId!=""){
                    that.api.sdk.getProductInfo(that.productId,function(info){
                        new prodlinkView({
                            el: that.el,
                            product:info
                        });
                        setTimeout(function(){
                            that.$el.scrollTop(scrollTop);
                        },0);
                    });

                }

            });
        });


        this.subviews = {};

        this.listenTo(this.collection, 'reset', this.addAll);
        this.listenTo(this.collection, 'add', this.addOne);
        this.listenTo(this.collection, 'remove', this.removeOne);



    },
    render: function(){
        this.collection.reset(this.collection.toJSON())

    },
    removeOne:function(todo){
        if(this.subviews[todo.get('id')]){
            this.subviews[todo.get('id')].remove();
            delete this.subviews[todo.get('id')];
        }
    },
    addOne: function(todo) {
        var that = this;
        var _todo = todo.toJSON();
        _todo.msg = util.escape(_todo.msg);
        todo.set({_msg:_todo.msg,msg:chatImgUtil.code2html(_todo.msg)});
        var view = new ChatMsgView({model: todo},this.api,this.uid,this.myHeadImg,this.otherHeadImg);
        this.$el.append(view.$el);
        setTimeout(function(){
            that.$el.scrollTop(scrollTop);
        },0);

        this.subviews[todo.get('id')] = view;
    },
    addAll: function() {
        if(this.collection.length>0){
            this.addTimestamp("",this.collection.first().toJSON().timestamp);
        }
        this.collection.each(this.addOne, this);
    },
    addMessage:function(msg){
        this.collection.add(msg);
    },
    addHistoryMsg:function(msg){
        msg.each(function(todo){
            var _todo = todo.toJSON();
            todo.set({_msg:_todo.msg,msg:chatImgUtil.code2html(_todo.msg)});
            var view = new ChatMsgView({model: todo},this.api,this.uid,this.myHeadImg,this.otherHeadImg);
            this.$el.prepend(view.render().el);
            //this.$el.scrollTop(scrollTop);
            this.subviews[todo.get('id')] = view;
        }, this);
        if(msg.length>0){
            this.addHistoryTimestamp(msg.first().toJSON().timestamp);
        }
    },
    addTimestamp:function(historyDate,newDate){
        var timeStampView = new TimeStampView({
            el: this.$el,
            collection: ""
        });
        if(timeStampView.isAddTimestamp(historyDate)){
            timeStampView.addTimestamp(newDate);
        }
    },
    addHistoryTimestamp:function(hisDate){
        var timeStampView = new TimeStampView({
            el: this.$el,
            collection: ""
        });
        timeStampView.addHIstoryTimestamp(hisDate);
    }
});